package com.vaadin.thomas.contenttool.ui;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import com.vaadin.data.Item;
import com.vaadin.terminal.ExternalResource;
import com.vaadin.thomas.contenttool.ContentTool;
import com.vaadin.thomas.contenttool.data.Model;
import com.vaadin.thomas.contenttool.data.Version;
import com.vaadin.thomas.contenttool.ui.events.LangChangedEvent;
import com.vaadin.thomas.contenttool.ui.events.LangChangedListener;
import com.vaadin.thomas.contenttool.ui.events.PreviewCommittedEvent;
import com.vaadin.thomas.contenttool.ui.events.PreviewCommittedListener;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.NativeSelect;

public class Header extends HorizontalLayout implements LangChangedListener,
		PreviewCommittedListener {

	private static final String PROPID_CAPTION = "caption";

	private static final long serialVersionUID = 6541443967515935079L;

	private Label title;
	private Button commit;
	private Button info;
	private Button preview;
	private NativeSelect version;

	private Label versionCaption;

	public Header() {

		setSpacing(true);
		setMargin(true);
		setWidth(100, UNITS_PERCENTAGE);
		addStyleName("header");

		title = new Label();
		title.addStyleName("title");
		title.setSizeUndefined();
		addComponent(title);
		setExpandRatio(title, 1);

		versionCaption = new Label();
		versionCaption.setSizeUndefined();
		addComponent(versionCaption);
		setComponentAlignment(versionCaption, Alignment.MIDDLE_CENTER);

		version = new NativeSelect();
		version.addContainerProperty(PROPID_CAPTION, String.class, "");
		version.setItemCaptionPropertyId(PROPID_CAPTION);
		version.setNullSelectionAllowed(false);
		version.addStyleName("version");
		addComponent(version);
		setComponentAlignment(version, Alignment.MIDDLE_CENTER);

		info = new Button();
		info.addStyleName("info");
		addComponent(info);

		preview = new Button();
		preview.addStyleName("preview");
		preview.addListener(new ClickListener() {

			private static final long serialVersionUID = -8761479254930396443L;

			@Override
			public void buttonClick(ClickEvent event) {

				String url = MainLayout.ct().conf().appURL();
				if (!url.endsWith("/")) {
					url += "/";
				}
				url += ContentTool.PREVIEW_URIFRAGMENT;
				getApplication().getMainWindow().open(
						new ExternalResource(url), "_blank");

			}
		});
		addComponent(preview);
		setExpandRatio(preview, 0.5f);

		commit = new Button();
		commit.addStyleName("preview");
		commit.addListener(new ClickListener() {

			private static final long serialVersionUID = 2748064670464979332L;

			@Override
			public void buttonClick(ClickEvent event) {
				getWindow().addWindow(new CommitWindow());
			}
		});
		addComponent(commit);

		previewCommited(null);
		langChanged(null);
	}

	@Override
	public void langChanged(LangChangedEvent event) {

		String userName = MainLayout.ct().getUserNameForUI();
		title.setValue(MainLayout.ct().get("contenttool.header.title") + ", "
				+ userName);
		preview.setCaption(MainLayout.ct().get("contenttool.header.preview"));
		versionCaption.setCaption(MainLayout.ct().get(
				"contenttool.header.version"));
		info.setCaption(MainLayout.ct().get("contenttool.header.info"));
		commit.setCaption(MainLayout.ct().get("contenttool.header.commit"));

		version.getItem(Model.get().getPreview())
				.getItemProperty(PROPID_CAPTION)
				.setValue(
						MainLayout.ct().get(
								"contenttool.header.version-preview"));

		Version prod = Model.get().getProduction();
		if (prod != null) {
			version.getItem(prod)
					.getItemProperty(PROPID_CAPTION)
					.setValue(
							MainLayout.ct().get(
									"contenttool.header.version-production"));
		}
	}

	private void refreshVersionSelect() {
		version.removeAllItems();

		DateFormat df = new SimpleDateFormat("dd.MM.yyyy");

		for (Version v : Model.get().getVersions()) {

			Item i = version.addItem(v);
			if (v.isPreview()) {
				i.getItemProperty(PROPID_CAPTION).setValue("");
			} else {
				i.getItemProperty(PROPID_CAPTION).setValue(
						v.getVersionNumber() + " - "
								+ df.format(v.getCommitDate()));
			}
		}

		version.select(Model.get().getPreview());
	}

	@Override
	public void previewCommited(PreviewCommittedEvent event) {
		refreshVersionSelect();
		langChanged(null);
	}

}
